﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using FU.Capstones.IMS.Services.Common;
using FU.Capstones.IMS.Services.Common.DataContracts;
using FU.Capstones.IMS.Services.Common.Respondents;
using FU.Capstones.IMS.Services.Interfaces;

namespace FU.Capstones.IMS.Services.DAOs
{
    public class PartnerDAO : DAOBase, IPartnerDAO
    {
        #region IPartnerDAO Members

        public CollectionFeedback<Partner> GetCollection(bool isShowAll)
        {
            var feedback = new CollectionFeedback<Partner> {IsSuccessful = false};
            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionPartner", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsShowAll", isShowAll);


                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    var partners = new List<Partner>();
                    while (reader.Read())
                    {
                        var partner = new Partner();

                        partner.PartnerID = Convert.ToInt32(reader["PartnerID"]);
                        partner.CompanyName = Convert.ToString(reader["CompanyName"]);
                        partner.PartnerCode = Convert.ToString(reader["PartnerCode"]);
                        partner.OfficePhone = Convert.ToString(reader["OfficePhone"]);
                        partner.MobilePhone = Convert.ToString(reader["MobilePhone"]);
                        partner.Address = Convert.ToString(reader["Address"]);
                        partner.Email = Convert.ToString(reader["Email"]);
                        partner.Fax = Convert.ToString(reader["Fax"]);
                        partner.Rating = Convert.ToInt32(reader["Rating"]);
                        partner.Description = reader["Description"].ToString();
                        partner.IsSupplier = Convert.ToBoolean(reader["IsSupplier"]);
                        partner.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                        partners.Add(partner);
                    }

                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                    feedback.Data = partners;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                    feedback.IsSuccessful = true;
                    feedback.Data = null;
                }

                Log.Info("GetPartnerCollection: {0}", DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("GetPartnerCollection - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                Log.Error("GetPartnerCollection - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<Partner> GetCollection2(bool? isSupplier)
        {
            var feedback = new CollectionFeedback<Partner> {IsSuccessful = false};
            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionPartnerByParam", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@isSupplier", isSupplier);

                SqlDataReader reader = command.ExecuteReader();

                var partners = new List<Partner>();

                while (reader.Read())
                {
                    var partner = new Partner();

                    partner.PartnerID = Convert.ToInt32(reader["PartnerID"]);
                    partner.CompanyName = Convert.ToString(reader["CompanyName"]);
                    partner.PartnerCode = Convert.ToString(reader["PartnerCode"]);
                    partner.OfficePhone = Convert.ToString(reader["OfficePhone"]);
                    partner.MobilePhone = Convert.ToString(reader["MobilePhone"]);
                    partner.Address = Convert.ToString(reader["Address"]);
                    partner.Email = Convert.ToString(reader["Email"]);
                    partner.Fax = Convert.ToString(reader["Fax"]);
                    partner.Rating = Convert.ToInt32(reader["Rating"]);
                    partner.Description = reader["Description"].ToString();
                    partner.IsSupplier = Convert.ToBoolean(reader["IsSupplier"]);
                    partner.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                    partners.Add(partner);
                }

                feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                feedback.IsSuccessful = true;
                feedback.Data = partners;

                Log.Info("GetPartnerCollection: {0}", DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("GetPartnerCollection - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                Log.Error("GetPartnerCollection - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<Partner> GetCollectionType(bool isShowAll)
        {
            var feedback = new CollectionFeedback<Partner> {IsSuccessful = false};
            try
            {
                OpenConnection();

                var command = new SqlCommand("GetCollectionPartner", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsShowAll", isShowAll);


                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    var partners = new List<Partner>();
                    while (reader.Read())
                    {
                        var partner = new Partner();

                        partner.PartnerID = Convert.ToInt32(reader["PartnerID"]);
                        partner.CompanyName = Convert.ToString(reader["CompanyName"]);
                        partner.PartnerCode = Convert.ToString(reader["PartnerCode"]);
                        partner.OfficePhone = Convert.ToString(reader["OfficePhone"]);
                        partner.MobilePhone = Convert.ToString(reader["MobilePhone"]);
                        partner.Address = Convert.ToString(reader["Address"]);
                        partner.Email = Convert.ToString(reader["Email"]);
                        partner.Fax = Convert.ToString(reader["Fax"]);
                        partner.Rating = Convert.ToInt32(reader["Rating"]);
                        partner.Description = reader["Description"].ToString();
                        partner.IsSupplier = Convert.ToBoolean(reader["IsSupplier"]);
                        partner.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                        partners.Add(partner);
                    }

                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                    feedback.Data = partners;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                    feedback.IsSuccessful = true;
                    feedback.Data = null;
                }

                Log.Info("GetPartnerCollection: {0}", DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("GetPartnerCollection - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                Log.Error("GetPartnerCollection - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<Partner> GetSingle(int objectID)
        {
            var feedback = new SingleFeedback<Partner> {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("GetSinglePartner", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@PartnerID", objectID);

                OpenConnection();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        var partner = new Partner();

                        partner.PartnerID = Convert.ToInt32(reader["PartnerID"]);
                        partner.PartnerCode = Convert.ToString(reader["PartnerCode"]);
                        partner.Address = Convert.ToString(reader["Address"]);
                        partner.CompanyName = Convert.ToString(reader["CompanyName"]);
                        partner.Description = Convert.ToString(reader["Description"]);
                        partner.Rating = Convert.ToInt32(reader["Rating"]);
                        partner.OfficePhone = Convert.ToString(reader["OfficePhone"]);
                        partner.MobilePhone = Convert.ToString(reader["MobilePhone"]);
                        partner.IsSupplier = Convert.ToBoolean(reader["IsSupplier"]);
                        partner.Fax = Convert.ToString(reader["Fax"]);
                        partner.Email = Convert.ToString(reader["Email"]);
                        partner.IsDelete = Convert.ToBoolean(reader["IsDelete"]);

                        feedback.Data = partner;

                        break;
                    }

                    if (reader.NextResult())
                    {
                        feedback.Data.ContactCollection = new List<Contact>();
                        while (reader.Read())
                        {
                            var contact = new Contact();
                            contact.ContactID = Convert.ToInt32(reader["ContactID"]);
                            contact.FullName = Convert.ToString(reader["FullName"]);
                            contact.MobilePhone = Convert.ToString(reader["MobilePhone"]);

                            feedback.Data.ContactCollection.Add(contact);
                        }
                    }

                    feedback.Message = InventoryConstants.Messages.GetSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.NA;
                    feedback.IsSuccessful = true;
                    feedback.Data = null;
                }

                Log.Info("GetSinglePartner: {0} - {1}", objectID, DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("GetSinglePartner - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                Log.Error("GetSinglePartner - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public SingleFeedback<int> Insert(Partner data)
        {
            var feedback = new SingleFeedback<int> {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("InsertPartner", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsSupplier", data.IsSupplier);
                command.Parameters.AddWithValue("@CompanyName", data.CompanyName);
                command.Parameters.AddWithValue("@Description", data.Description);
                command.Parameters.AddWithValue("@PartnerCode", data.PartnerCode);
                command.Parameters.AddWithValue("@Address", data.Address);
                command.Parameters.AddWithValue("@Email", data.Email);
                command.Parameters.AddWithValue("@Fax", data.Fax);
                command.Parameters.AddWithValue("@MobilePhone", data.MobilePhone);
                command.Parameters.AddWithValue("@OfficePhone", data.OfficePhone);
                command.Parameters.AddWithValue("@Rating", data.Rating);

                OpenConnection();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        int partnerID = Convert.ToInt32(reader["PartnerID"]);

                        feedback.Data = partnerID;
                        feedback.Message = InventoryConstants.Messages.InsertSuccessfully;
                        feedback.IsSuccessful = true;

                        break;
                    }
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.InsertFailed;
                    feedback.IsSuccessful = false;
                    feedback.Data = -1;
                }

                Log.Info("InsertPartner: {0} - {1}", feedback.Data, DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("InsertPartner - SqlException: {0} - {1}", se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                Log.Error("InsertPartner - Exception: {0} - {1}", e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Update(Partner data)
        {
            var feedback = new Feedback {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("UpdatePartner", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@PartnerID", data.PartnerID);
                command.Parameters.AddWithValue("@CompanyName", data.CompanyName);
                command.Parameters.AddWithValue("@Description", data.Description);
                command.Parameters.AddWithValue("@Address", data.Address);
                command.Parameters.AddWithValue("@PartnerCode", data.PartnerCode);
                command.Parameters.AddWithValue("@Email", data.Email);
                command.Parameters.AddWithValue("@Fax", data.Fax);
                command.Parameters.AddWithValue("@MobilePhone", data.MobilePhone);
                command.Parameters.AddWithValue("@OfficePhone", data.OfficePhone);
                command.Parameters.AddWithValue("@Rating", data.Rating);

                OpenConnection();

                int rowCount = command.ExecuteNonQuery();
                if (rowCount == 1)
                {
                    feedback.Message = InventoryConstants.Messages.UpdateSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.UpdateFailed;
                    feedback.IsSuccessful = false;
                }

                Log.Info("UpdatePartner: {0} - {1} - {2}", data.PartnerID, feedback.Message, DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("UpdatePartner - SqlException: {0} - {1} - {2}", data.PartnerID, se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                Log.Error("UpdatePartner - Exception: {0} - {1} - {2}", data.PartnerID, e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback Delete(int objID)
        {
            var feedback = new Feedback {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("DeletePartner", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@PartnerID", objID);

                OpenConnection();

                int rowCount = command.ExecuteNonQuery();
                if (rowCount == 1)
                {
                    feedback.Message = InventoryConstants.Messages.DeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.DeleteFailed;
                    feedback.IsSuccessful = false;
                }

                Log.Info("DeletePartner: {0} - {1} - {2}", objID, feedback.Message, DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("DeletePartner - SqlException: {0} - {1} - {2}", objID, se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                Log.Error("DeletePartner - Exception: {0} - {1} - {2}", objID, e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public CollectionFeedback<Partner> Search(Partner criteria)
        {
            var feedback = new CollectionFeedback<Partner> {IsSuccessful = false, Data = new List<Partner>()};

            try
            {
                OpenConnection();

                var command = new SqlCommand("SearchPartner", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@IsSupplier",
                                                criteria.IsSupplier.HasValue
                                                    ? (object) criteria.IsSupplier.Value
                                                    : DBNull.Value);
                command.Parameters.AddWithValue("@PartnerCode",
                                                string.IsNullOrEmpty(criteria.PartnerCode)
                                                    ? criteria.PartnerCode
                                                    : criteria.PartnerCode.Trim());
                command.Parameters.AddWithValue("@CompanyName",
                                                string.IsNullOrEmpty(criteria.CompanyName)
                                                    ? criteria.CompanyName
                                                    : criteria.CompanyName.Trim());
                command.Parameters.AddWithValue("@Address",
                                                string.IsNullOrEmpty(criteria.Address)
                                                    ? criteria.Address
                                                    : criteria.Address.Trim());

                SqlDataReader reader = command.ExecuteReader();

                var partners = new List<Partner>();

                while (reader.Read())
                {
                    var partner = new Partner();
                    partner.PartnerID = Convert.ToInt32(reader["PartnerID"]);
                    partner.PartnerCode = Convert.ToString(reader["PartnerCode"]);
                    partner.CompanyName = Convert.ToString(reader["CompanyName"]);
                    partner.Address = Convert.ToString(reader["Address"]);
                    partner.Email = Convert.ToString(reader["Email"]);
                    partner.Fax = Convert.ToString(reader["Fax"]);
                    partner.Description = Convert.ToString(reader["Description"]);
                    partner.MobilePhone = Convert.ToString(reader["MobilePhone"]);
                    partner.OfficePhone = Convert.ToString(reader["OfficePhone"]);
                    partner.IsDelete = Convert.ToBoolean(reader["IsDelete"]);
                    partner.Rating = Convert.ToInt32(reader["Rating"]);
                    partner.IsSupplier = Convert.ToBoolean(reader["IsSupplier"]);

                    partners.Add(partner);
                }
                feedback.Message = InventoryConstants.Messages.SearchSuccessfully;
                feedback.IsSuccessful = true;
                feedback.Data = partners;

                Log.Info("SearchPartner: {0}, {1}", feedback.Message, DateTime.Now.ToString());
            }
            catch (SqlException e)
            {
                Log.Error(e.Message);
                feedback.Message = InventoryConstants.Messages.SearchFailed;
            }
            catch (Exception t)
            {
                Log.Error(t.Message);
                feedback.Message = InventoryConstants.Messages.SearchFailed;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        public Feedback UndoDelete(int objID)
        {
            var feedback = new Feedback {IsSuccessful = false};
            try
            {
                var command = new SqlCommand("UndoDeletedPartner", imsConnection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@PartnerID", objID);

                OpenConnection();

                int rowCount = command.ExecuteNonQuery();
                if (rowCount == 1)
                {
                    feedback.Message = InventoryConstants.Messages.UndoDeleteSuccessfully;
                    feedback.IsSuccessful = true;
                }
                else
                {
                    feedback.Message = InventoryConstants.Messages.UndoDeleteFailed;
                    feedback.IsSuccessful = false;
                }

                Log.Info("UndoDeletedPartner: {0} - {1} - {2}", objID, feedback.Message, DateTime.Now);
            }
            catch (SqlException se)
            {
                Log.Error("UndoDeletedPartner - SqlException: {0} - {1} - {2}", objID, se.StackTrace, DateTime.Now);
                feedback.Message = se.Message;
            }
            catch (Exception e)
            {
                Log.Error("UndoDeletedPartner - Exception: {0} - {1} - {2}", objID, e.StackTrace, DateTime.Now);
                feedback.Message = e.Message;
            }
            finally
            {
                CloseConnection();
            }
            return feedback;
        }

        #endregion
    }
}